package com.mars.activemq.consumer;

import java.io.File;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import com.mars.tukuan.dto.CarMessage;
import com.mars.tukuan.entity.CarState;
import com.mars.tukuan.rpc.api.CarService;
import com.mars.tukuan.rpc.api.CarStateService;
import com.mars.tukuan.rpc.api.CarUserService;
import com.mars.tukuan.utils.file.FileMeta;
import com.mars.tukuan.utils.file.FileUtil;
import com.mars.tukuan.utils.file.cloud.UploadCloudStroe;
import com.mars.tukuan.utils.file.cloud.UploadResult;

/**
 * 处理地勤用户上传车况信息
 * @author yhaoquan
 *
 */
@Component
public class CarStateConsumer {
	private static Logger logger = LoggerFactory.getLogger(CarStateConsumer.class);
	
	@Autowired
	private CarService carService;
	
	@Autowired
	private CarStateService carStateService;
	
	@Autowired
	private CarUserService carUserService;
	
	@Autowired
	private UploadCloudStroe uploadCloudStroe;
	
	/**
	 * 处理前（新伤）
	 * @param carMessage
	 */
	@JmsListener(destination="car.beboreUpload")
	public void beboreUpload(CarMessage carMessage) {
		logger.debug("收到MQ异步消息 ，队列名称[car.beboreUpload]");
		try {
			
			CarState carState = carMessage.getCarState() ;
			List<FileMeta> fileMeta = carMessage.getFileMeta() ;
			
			String filePath = fileMeta.get(0).getDestFilePath() ;
			
			UploadResult result = this.uploadCloudStroe.uploadFile(new File(filePath)) ;
			if(result.getStatus()) {
				//删除临时目录下的文件
				FileUtil.deleteFile(filePath);
				
				carState.setBeforeImageUrl(result.getFileUrl());
				carState.setIsHarm(1);
				carState.setFollow(1);
				this.carStateService.insertSelective(carState) ;
				
				//更新车况检查时间
				this.carService.updateCheckTime(carState.getCarId());
				//更新用户的车况见时间
				this.carUserService.updateCheckTime(carState.getCarUserId());
			}
			
		} catch (Exception e) {
			logger.error("车况管理系统-MQ队列消息，处理新伤发生异常：{}", e.getMessage()) ;
		}
	}
	
	/**
	 * 处理后
	 * @param carMessage
	 */
	@JmsListener(destination="car.afterUpload")
	public void afterUpload(CarMessage carMessage) {
		logger.debug("收到MQ异步消息 ，队列名称[car.afterUpload]");
		try {
			
			CarState carState = carMessage.getCarState() ;
			List<FileMeta> fileMeta = carMessage.getFileMeta() ;
			
			String filePath = fileMeta.get(0).getDestFilePath() ;
			
			UploadResult result = this.uploadCloudStroe.uploadFile(new File(filePath)) ;
			if(result.getStatus()) {
				//删除临时目录下的文件
				FileUtil.deleteFile(filePath);
				
				CarState entity = this.carStateService.selectById(carState.getId()) ;
				entity.setAfterImageUrl(result.getFileUrl());
				entity.setAfterDesc(carState.getAfterDesc());
				entity.setAfterTime(new Date());
				entity.setAfterUserId(carState.getAfterUserId());
				entity.setIsHarm(1);
				entity.setFollow(1);
				this.carStateService.updateSelective(entity) ;
				
				//更新车况检查时间
				this.carService.updateCheckTime(carState.getCarId());
				//更新用户的车况见时间
				this.carUserService.updateCheckTime(carState.getCarUserId());
				
				logger.info("车况管理系统-MQ队列消息，添加“处理后”信息");
			}
		} catch (Exception e) {
			logger.error("车况管理系统-MQ队列消息，添加“处理后（新伤）”信息异常：", e.getMessage());
		}
	}
	
}
